package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController  {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);
    //开启学生管理系统且展示管理系统菜单
    public void start() {
        Studentol:
        while (true) {
            System.out.println("----------------欢迎来到学生管理系统---------------");
            System.out.println("请输入您的选择: 1.添加 2.删除 3.修改 4.查询 5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改");
                    updeteStudent();
                    break;
                case "4":
                    //System.out.println("查询");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统");
                    break Studentol;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    public void updeteStudent() {
        String updateId;
        while (true){
            //键盘录入要删除的学生ID
            System.out.println("请输入您要修改的学生id");
            updateId = sc.next();
            //判断ID在容器中是否存在，如果不存在，则需要一道录入
            boolean exists = studentService.isExists(updateId);
            if (!exists){
                System.out.println("您输入的id不存在，请重新输：");
            }else{
                break;
            }
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        String age = sc.next();
        System.out.println("请输入学生生日");
        String birthday = sc.next();

        Student newStu = new Student();
        newStu.setId(updateId);
        newStu.setName(name);
        newStu.setAge(age);
        newStu.setBirthday(birthday);

        studentService.updateStudent(updateId,newStu);
        System.out.println("修改成功~");
    }

    public void deleteStudentById() {
        String delId;
        while (true){
            //键盘录入要删除的学生ID
            System.out.println("请输入您要删除的学生id");
            delId = sc.next();
            //判断ID在容器中是否存在，如果不存在，则需要一道录入
            boolean exists = studentService.isExists(delId);
            if (!exists){
                System.out.println("您输入的id不存在，请重新输：");
            }else{
                break;
            }
        }
        //调用业务员中的deleteStudentById根据id，删除学生
        studentService.deleteStudentById(delId);
        System.out.println("删除成功~");
        //提示删除成功
    }

    public void findAllStudent() {
        //调用业务员中的获取方法，得到学生对象数组
        Student[] stus = studentService.findAllStudent();
        //判断数组的内存地址，是否为null
        if (stus == null){
            System.out.println("查无信息，请添加后重试");
            return;
        }
        //遍历数组，获取学生信息并打印在控制台
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null){
                System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t\t"+stu.getBirthday());
            }
        }
    }


    public void addStudent() {
        //StudentService studentService = new StudentService();
        //键盘接受学生信息
        //Scanner sc = new Scanner(System.in);
        String id;
        while (true){
            System.out.println("请输入学生id");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (flag){
                System.out.println("学号已被使用，请重新输入");
            }else{
                break;
            }
        }
        System.out.println("请输入学生名字");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        String age = sc.next();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
        //将学生信息封装为学生对象
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        //将学生对象传递给StudentService（业务员）中的addStudent方法
        boolean result = studentService.addStudent(stu);
        if (result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
}
